<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 * @version 1.0
 * @author xpl0it
 */
class Pon extends CI_Controller {

    public $table = "pon";
    public $stat_table = "stat_table";

    function __construct() {
        parent::__construct();
        $this->load->model('pon_model', 'model');
        $this->load->model('customer_area_model', 'customer_area_model');
        //$this->output->enable_profiler(TRUE);
    }

    function index() {
        $this->checkauth();
        $pon = $this->model;
        $data['query'] = $pon->select_by_table($this->table);
        $content = $this->load->view('pon/index', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render_dt', $content_data);
    }

    function add($data = array()) {
        $this->checkauth();
        $data = "";
        $content = $this->load->view('pon/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function update($param) {
        $this->checkauth();
        $data['val'] = $this->model->select_by_filter($param);
        $content = $this->load->view('pon/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function save() {
        $this->checkauth();

        $this->load->model('sdp_model');
        $this->load->model('line_service_model');

        if ($_POST['pon_id'] != '') {
            $rs = $this->model->update_data($this->input->post(NULL, TRUE));
            if ($rs) {
                $data['pon_id'] = $_POST['pon_id'];
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("แก้ไขข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/pon") . '";</script>';
                echo $data;
            } else {
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถแก้ไขข้อมูลได้ !");window.history.back();</script>';
                echo $data;
            }
        } else {
            $rs = $this->model->insert_data($this->input->post(NULL, TRUE));
            if ($rs) {
                $data['pon_id'] = $rs;

                //เพิ่มข้อมูล sdp
                for ($isdp = 1; $isdp <= $_POST['sdp_slot']; $isdp++) {
                    $valSdp = array();
                    $valSdp['sdp_code'] = "SDP#00{$isdp}";
                    $valSdp['pon_id'] = $data['pon_id'];
                    $sdpId = $this->sdp_model->insert($valSdp);

                    for ($ich = 1; $ich <= $_POST['sdp_ch']; $ich++) {
                        $valCh = array();
                        $valCh['sdp_id'] = $sdpId;
                        $valCh['CH'] = '1';
                        $valCh['ls_type'] = 'FO';
                        $chId = $this->line_service_model->insert($valCh);
                    }
                }

                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("บันทึกข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/pon") . '";</script>';
                echo $data;
            } else {
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถบันทึกข้อมูลได้ !");window.history.back();</script>';
                echo $data;
            }
        }
    }

    function del($id) {
        $this->checkauth();
        $rs = $this->model->delete_data($id);
        if ($rs) {
            $this->load->view('template/header');
            $data = '<script type="text/javascript">alert("ลบข้อมูล PON ้สำเร็จแล้ว ! ");location = "' . base_url("index.php/admin/pon") . '";</script>';
            echo $data;
        }
    }

    function profile($id) {
        $this->checkauth();
        $data['val'] = $this->model->select_by_filter($id);
        $data['area'] = $this->customer_area_model->select_by_filter_pon($id);

        $content = $this->load->view('pon/profile', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    private function checkauth() {
        if (!$this->session->userdata('authen')) {
            redirect('admin/login/', 'refresh');
        }
    }

}

/* End of file customer.php */
/* Location: ./application/controllers/admin/pon.php */